package com.siruier.boss.ui.widget

import android.app.Activity
import android.content.Context
import android.content.res.TypedArray
import android.graphics.Color
import android.graphics.Typeface
import android.graphics.drawable.ColorDrawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.ViewCompat
import com.siruier.boss.ui.R
import com.siruier.boss.ui.databinding.TitleBarLayoutBinding
import com.siruier.boss.ui.ext.toColor
import com.siruier.boss.ui.ext.visible

class TitleBarLayout : FrameLayout, View.OnClickListener {
    companion object {
        const val TBL_MAIN = 1 // 主色调
        const val TBL_LOGIN = 2 // 登录
        const val TBL_TRANSPARENT = 3 // 透明背景
    }

    private lateinit var viewBinding: TitleBarLayoutBinding
    private var mTblStyle: Int = TBL_MAIN

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init(attrs)
    }

    private fun init(attrs: AttributeSet?) {
        viewBinding = TitleBarLayoutBinding.inflate(LayoutInflater.from(context), this, true)
        attrs?.let {
            val typedArray: TypedArray =
                context.obtainStyledAttributes(attrs, R.styleable.TitleBarLayout)
            mTblStyle = typedArray.getInt(R.styleable.TitleBarLayout_tbl_style, mTblStyle)
            rightImage = typedArray.getResourceId(R.styleable.TitleBarLayout_right_image, 0)
            rightStr = typedArray.getString(R.styleable.TitleBarLayout_right_str) ?: ""
            rightTextColor = typedArray.getColor(
                R.styleable.TitleBarLayout_right_str_color,
                R.color.text_color.toColor()
            )
            titleStr = typedArray.getString(R.styleable.TitleBarLayout_title_str) ?: ""
            isTitleBold = typedArray.getBoolean(R.styleable.TitleBarLayout_is_title_bold, false)
            isShowLeft = typedArray.getBoolean(R.styleable.TitleBarLayout_is_show_left, true)
            typedArray.recycle()
        }
        when (mTblStyle) {
            TBL_MAIN -> {
                leftImage = R.mipmap.icon_blak_back
                titleTextColor = R.color.text_color.toColor()
                bgColor = ColorDrawable(R.color.colorPrimaryDark.toColor())
            }
            TBL_TRANSPARENT -> {
                leftImage = R.mipmap.new_nav_back
                titleTextColor = R.color.white.toColor()
                bgColor = ColorDrawable(Color.TRANSPARENT)
            }
//            TBL_LOGIN -> {
//                leftImage = R.mipmap.icon_blak_back
//                titleTextColor = R.color.text_color.toColor()
//                bgColor = ColorDrawable(R.color.white.toColor())
//                isTitleBold = true
//            }
        }
        viewBinding.ivBack.setOnClickListener(this)
        viewBinding.ivRightImage.setOnClickListener(this)
        viewBinding.tvRightText.setOnClickListener(this)
    }

    override fun onClick(v: View) {
        when (v) {
            viewBinding.ivBack -> {
                if (onLeftClickListener != null) {
                    onLeftClickListener?.invoke(v)
                } else {
                    if (context is Activity) (context as Activity).onBackPressed()
                }
            }
            viewBinding.ivRightImage -> onRightImageClickListener?.invoke(v)
            viewBinding.tvRightText -> onRightTextClickListener?.invoke(v)
        }
    }

    var onLeftClickListener: ((view: View) -> Unit)? = null
    var onRightImageClickListener: ((view: View) -> Unit)? = null
    var onRightTextClickListener: ((view: View) -> Unit)? = null

    fun setBgColor(color: Int) {
        bgColor = ColorDrawable(color)
    }

    var bgColor = ColorDrawable(R.color.colorPrimaryDark.toColor())
        set(value) = ViewCompat.setBackground(this, value)
    var titleTextColor: Int = R.color.white.toColor()
        set(value) = viewBinding.tvTitle.setTextColor(value)
    var leftImage = R.mipmap.new_nav_back
        set(value) = viewBinding.ivBack.setImageResource(value)
    var isShowLeft: Boolean = true
        set(value) {
            value.let { viewBinding.ivBack.visibility = if (it) View.VISIBLE else View.GONE }
            field = value
        }
    var titleStr: String = ""
        set(value) = value.let { viewBinding.tvTitle.text = value }
    var isTitleBold: Boolean = false
        set(value) = value.let {
            if (value) viewBinding.tvTitle.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
        }
    var rightImage: Int? = null
        set(value) {
            if (value != null && value > 0) {
                viewBinding.ivRightImage.setImageResource(value)
                viewBinding.ivRightImage.visible()
            }
            field = value
        }
    var rightTextColor: Int = R.color.text_color.toColor()
        set(value) = viewBinding.tvRightText.setTextColor(value)
    var rightStr: String = ""
        set(value) = value.let {
            if (it.isNotEmpty()) {
                viewBinding.tvRightText.text = value;viewBinding.tvRightText.visible()
            }
        }
    val titleTextView: TextView
        get() = viewBinding.tvTitle
    val rightTextView: TextView
        get() = viewBinding.tvRightText
    val rightImageView: ImageView
        get() = viewBinding.ivRightImage
}